﻿using Microsoft.Phone.Controls;
using NaverOpenApiLibrary.Services;
using NaverOpenApiLibrary.Models;

namespace NaverOpenApiSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            System.Threading.Thread th = new System.Threading.Thread(doThreading);
            th.Start();
        }


        // ****************************************************
        // REPLACE BELOW WITH YOUR API KEY
        // API Keys can be issued at: http://dev.naver.com/openapi/
        private const string SHORT_URL_API_KEY = "f9b1b8aed4bbe2949992ca72023dfed7";
        private const string SEARCH_API_KEY = "4f60b03829def6e96147d436882f10ec";

        private void doThreading()
        {
            UrlShortnerService urlService = new UrlShortnerService(SHORT_URL_API_KEY);
            urlService.ApiServiceCompleted += (s, u) =>
            {
                if (u.ResultCode != null)
                    System.Diagnostics.Debug.WriteLine(u.Urls.ShortUrl);
                else
                    System.Diagnostics.Debug.WriteLine("[MainPage] Invalid API request or API key");
            };
            urlService.DoShorten("http://dev.naver.com/");

            HotTopicsService hotTopicService = new HotTopicsService(SEARCH_API_KEY);
            hotTopicService.ApiServiceCompleted += (s, h) =>
            {
                if (h != null)
                    for (int i = 0; i < 10; i++)
                        System.Diagnostics.Debug.WriteLine(h.HotTopics[i].Keyword);
                //foreach (HotTopic topic in h.HotTopics)
                //    System.Diagnostics.Debug.WriteLine(topic.Keyword);
                else
                    System.Diagnostics.Debug.WriteLine("[MainPage] Invalid API request or API key");
            };
            hotTopicService.GetHotTopics();

            NewsSearchService newsSearcher = new NewsSearchService(SEARCH_API_KEY);
            newsSearcher.ApiServiceCompleted += (s, n) =>
            {
                if (n != null)
                    //for (int i = 0; i < 20; i++)
                    //    System.Diagnostics.Debug.WriteLine(n.Channel.Item[i].Title);
                    foreach (NewsDetail news in n.Channel.Item)
                        System.Diagnostics.Debug.WriteLine(news.Title);
                else
                    System.Diagnostics.Debug.WriteLine("[MainPage] Invalid API request or API key");
            };
            newsSearcher.GetNews("대선", 1, 20);

            KinService kinSearcher = new KinService(SEARCH_API_KEY);
            kinSearcher.ApiServiceCompleted += (s, k) =>
                {
                    if (k != null)
                        foreach (KinItem kin in k.Channel.Items)
                            System.Diagnostics.Debug.WriteLine(kin.Title);
                    else
                        System.Diagnostics.Debug.WriteLine("[MainPage] Invalid API request or API key");
                    
                };
            kinSearcher.GetKin("롯데월드", 1, 20, KinService.Sort.Count);

            BlogService blogSearcher = new BlogService(SEARCH_API_KEY);
            blogSearcher.ApiServiceCompleted += (s, b) =>
                {
                    if (b != null)
                        foreach (BlogItem blog in b.Channel.Items)
                            System.Diagnostics.Debug.WriteLine(blog.Title);
                    else
                        System.Diagnostics.Debug.WriteLine("[MainPage] Invalid API request or API key");
                };
            blogSearcher.GetBlog("에버랜드");
        }
    }
}